'''
Created on Aug 22, 2013

@author: marchi
'''
import re;
from Data import *;
from math import *; 

SubColumns=7;

class AreaRes:
    def __init__(self,filename):
        p=re.compile('^%\$AreaRes');
        self.Table={};
        self.count=0;
        self.all={};
        self.Nmax=0;
        for line in open(filename):
            if p.match(line):
                LineL=line.split();
                if len(LineL) != 1:
                    Nmax=(len(LineL)-7)/2;
                    nst1=4;nend1=4+Nmax;
                    nst2=nend1+3;nend2=len(LineL);
                    rng1=range(nst1,nend1);
                    rng2=range(nst2,nend2);
                    Nkey1=1;
                    Nkey2=1+Nmax+3;

                    self.count+=1;
                    Tabs={};
                    MyL=[LineL[Nkey1], LineL[Nkey2]];
                    Tabs[MyL[0]]=MyData(Nmax);
                    Tabs[MyL[1]]=MyData(Nmax);
                    Tabs[MyL[0]].copy([float(LineL[o]) for o in rng1]);
                    Tabs[MyL[1]].copy([float(LineL[o]) for o in rng2]);
                    MyL=[LineL[Nkey1], LineL[Nkey2]];
                    for obj in MyL:
                        if not self.Table.has_key(obj):
                            self.Table[obj]=[MyData(Nmax),0];
                            self.all[obj]=[[o] for o in Tabs[obj].data];

                        self.Table[obj][0]=self.Table[obj][0]+Tabs[obj];
                        self.Table[obj][1]+=1;
                        for o in range(Nmax):
                            self.all[obj][o].append(Tabs[obj].data[o]);
        self.Nmax=Nmax;

    def Avg(self):
        for obj in enumerate(self.Table):
            self.Table[obj[1]][0]=self.Table[obj[1]][0]/self.Table[obj[1]][1];
        keylist=self.Table.keys();
        keylist.sort();
        for key in keylist:
            st1=' %s ' %(key);
            for i in range(len(self.Table[key][0].data)):
                st1+=' %10.4f ' % (self.Table[key][0].data[i]);
            print(st1);
            
            
    def Hist(self,dx,rnge):
        self.histo={};
        self.dx=dx;
        if not isinstance(rnge, list): return;
        keylist=self.all.keys();
        print self.Nmax;
        self.nhist=int(ceil((rnge[1]-rnge[0])/dx))+1;
        n=self.nhist
        for obj in keylist:
            histo=[0.0 for o in range(self.Nmax)];
            for o in range(self.Nmax):
                histo[o]=[0.0 for p in range(n)];

            for o in range(self.Nmax):
                for p in range(len(self.all[obj][o])):
                    m=int(ceil((self.all[obj][o][p]-rnge[0])/dx));
                    if m < n:
                        histo[o][m]+=1.0;
                    else:
                        histo[o][n-1]+=1.0;
                
            for o in range(self.Nmax):
                sum=0.0;
                for p in range(n):
                    sum+=histo[o][p];
                sum*=dx;
                Norm=1.0/sum;
                for p in range(n):
                    histo[o][p]*=Norm;
            self.histo[obj]=histo;
        
        str0='Created two histograms data '
        for obj in keylist:
            str0+=obj+ '  '
        print str0;

    def pHisto(self,obj):
        n=self.nhist
        if self.histo.has_key(obj):
            for p in range(n):
                ss=' %8.3f ' % (float(p*self.dx));
                ok=False;
                for o in range(self.Nmax):
                    if self.histo[obj][o][p] != 0: ok=True;
                if ok:
                    for o in range(self.Nmax):
                        ss+=' %f ' % (self.histo[obj][o][p]);
                    print ss; 
                else:
                    continue;
